package com.yang.web.conf;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * <p>@ProjectName:spring-boot-web-demo</p>
 * <p>@Package:com.yang.web.conf</p>
 * <p>@ClassName:MyLocalResolver</p>
 * <p>@Description:</p>
 * <p>@Author:yang</p>
 * <p>@author:yang</p>
 * <p>@Date:2022/1/4 9:47</p>
 * <p>@Version:1.0</p>
 */
public class MyLocalResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String parameter = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(parameter)) {
            String[] split = parameter.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, @Nullable HttpServletResponse httpServletResponse, @Nullable Locale locale) {

    }

}
